import react from '@vitejs/plugin-react'
import * as path from 'path'
import * as dotenv from 'dotenv'
import * as fs from 'fs'

export default ({
  mode
}) => {
  const NODE_ENV = mode || 'development'
  const envFile = `.env.${NODE_ENV}`
  const envConfig = dotenv.parse(fs.readFileSync(envFile))
  for (const k in envConfig) {
    process.env[k] = envConfig[k]
  }

  return {
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      }
    },
    define: {
      'process.env': {},
    },
    server: {
      port: 8000,
    },
    base:'/',
    build: {
      manifest: false,
      sourcemap: false,
      outDir: 'build',
      rollupOptions: {
        output: {
          entryFileNames: '087AC4D[hash].js',
          chunkFileNames: '087AC4D[hash].js',
          assetFileNames: '087AC4D[hash].[ext]',
        }
      },
    },
    esbuild: {},
    optimizeDeps: {},
    plugins: [
      react()
    ]
  }
}
